package com.macro.mall.portal.service.impl;

import com.macro.mall.mapper.PmsBrandMapper;
import com.macro.mall.mapper.UmsMemberBrandAttentionMapper;
import com.macro.mall.model.PmsBrand;
import com.macro.mall.model.UmsMember;
import com.macro.mall.model.UmsMemberBrandAttention;
import com.macro.mall.model.UmsMemberBrandAttentionExample;
import com.macro.mall.portal.service.MemberAttentionService;
import com.macro.mall.portal.service.UmsMemberService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 会员关注Service实现类
 *
 * @author macro
 * @date 2018/8/2
 */
@Service
@AllArgsConstructor
public class MemberAttentionServiceImpl implements MemberAttentionService {
    private final PmsBrandMapper brandMapper;
    private final UmsMemberService memberService;
    private final UmsMemberBrandAttentionMapper umsMemberBrandAttentionMapper;

    @Override
    public int add(UmsMemberBrandAttention memberBrandAttention) {
        int count = 0;
        if (memberBrandAttention.getBrandId() == null) {
            return 0;
        }
        UmsMember member = memberService.getCurrentMember();
        memberBrandAttention.setMemberId(member.getId());
        memberBrandAttention.setMemberNickname(member.getNickname());
        memberBrandAttention.setMemberIcon(member.getIcon());
        memberBrandAttention.setCreateTime(new Date());

        UmsMemberBrandAttentionExample example = new UmsMemberBrandAttentionExample();
        example.createCriteria()
               .andMemberIdEqualTo(memberBrandAttention.getMemberId())
               .andBrandIdEqualTo(memberBrandAttention.getBrandId());
        List<UmsMemberBrandAttention> umsMemberBrandAttentionList = umsMemberBrandAttentionMapper.selectByExample(example);

        if (umsMemberBrandAttentionList.isEmpty()) {
            PmsBrand brand = brandMapper.selectByPrimaryKey(memberBrandAttention.getBrandId());
            if (brand == null) {
                return 0;
            } else {
                memberBrandAttention.setBrandCity(null);
                memberBrandAttention.setBrandName(brand.getName());
                memberBrandAttention.setBrandLogo(brand.getLogo());
            }
            return umsMemberBrandAttentionMapper.insertSelective(memberBrandAttention);
        }
        return count;
    }

    @Override
    public int delete(Long brandId) {
        UmsMember member = memberService.getCurrentMember();
        UmsMemberBrandAttentionExample example = new UmsMemberBrandAttentionExample();
        example.createCriteria()
               .andMemberIdEqualTo(member.getId())
               .andBrandIdEqualTo(brandId);
        return umsMemberBrandAttentionMapper.deleteByExample(example);
    }

    @Override
    public List<UmsMemberBrandAttention> list(Integer pageNum, Integer pageSize) {
        UmsMember member = memberService.getCurrentMember();
        UmsMemberBrandAttentionExample example = new UmsMemberBrandAttentionExample();
        example.createCriteria()
               .andMemberIdEqualTo(member.getId());
        example.setOrderByClause("create_time desc");
        return umsMemberBrandAttentionMapper.selectByExample(example);
    }

    @Override
    public UmsMemberBrandAttention detail(Long brandId) {
        UmsMember member = memberService.getCurrentMember();
        UmsMemberBrandAttentionExample example = new UmsMemberBrandAttentionExample();
        example.createCriteria()
               .andBrandIdEqualTo(brandId)
               .andMemberIdEqualTo(member.getId());
        List<UmsMemberBrandAttention> umsMemberBrandAttentions = umsMemberBrandAttentionMapper.selectByExample(example);
        return umsMemberBrandAttentions.isEmpty() ? null : umsMemberBrandAttentions.get(0);
    }

    @Override
    public void clear() {
        UmsMember member = memberService.getCurrentMember();
        UmsMemberBrandAttentionExample example = new UmsMemberBrandAttentionExample();
        example.createCriteria()
               .andMemberIdEqualTo(member.getId());
        umsMemberBrandAttentionMapper.deleteByExample(example);
    }
}
